package tcln.sors.client.core.component.grid;

import javax.swing.JTable;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.table.DefaultTableColumnModelExt;
import org.jdesktop.swingx.table.TableColumnExt;

import tcln.sors.client.core.mvc.ModelData;

/**
 * Extension of {@link JGrid} to work together with <b>SwingX</b> framework.
 * 
 * @param <M>
 *            Model data type.
 * @author Truong Chau Lien Ngoc (lienngoc@gmail.com)
 */
@SuppressWarnings("serial")
public class JXGrid<M extends ModelData<?>> extends JGrid<M> {

    /**
     * {@inheritDoc}
     */
    @Override
    protected final JXTable createTable() {
        JXTable table = new ExtendedJXTable();
        initializeTable(table);
        return table;
    }

    /**
     * Initializes given table just after a new {@link JTable} instance has been created inside method
     * {@link #createTable()}.
     */
    protected void initializeTable(JXTable table) {
        super.initializeTable(table);
    }

    /**
     * {@inheritDoc}
     * 
     * @deprecated use {@link #initializeTable(JXTable)} instead.
     */
    @Override
    @Deprecated
    protected void initializeTable(JTable table) {
        super.initializeTable(table);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected TableColumn createNewColumn() {
        return new TableColumnExt();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected TableColumnModel createColumnModel() {
        return new DefaultTableColumnModelExt();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected DefaultTableColumnModelExt getColumnModel() {
        return (DefaultTableColumnModelExt) super.getColumnModel();
    }

    /**
     * Extensions to {@link JXTable} to make it work with {@link JXGrid}.
     * 
     * @author Truong Chau Lien Ngoc (lienngoc@gmail.com)
     */
    protected class ExtendedJXTable extends JXTable {
        @Override
        protected boolean getControlsSorterProperties() {
            return isRendered() ? super.getControlsSorterProperties() : false;
        }
    }
}
